package com.content.view.Component.OnlineRes
{
	import com.content.model.LoadedAssets.FontProxy;
	import com.content.model.OnlineRes.OnlineResVO;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	import com.library.utils.TextFieldUtil;
	
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	public class ResItem extends UIObject implements IDisposable
	{
		public static const WIDTH:int = 110+10;
		public static const HEIGHT:int = 140+10;
		private var _isActive:Boolean;
		
		protected var _container:Sprite;
		private var _bg:Shape;
		private var _vo:OnlineResVO;
		private var _coverLoader:Loader;
		protected var _text:TextField;
		
		public function ResItem(vo:OnlineResVO)
		{
			super();
			
			mouseChildren = false;
			buttonMode = true;
			
			_vo = vo;
			
			_container = new Sprite();
			addChild(_container);
			
			_bg = new Shape();
			_container.addChild(_bg);
			
			_bg.graphics.beginFill(0xffffff, 1.0);
			_bg.graphics.drawRect(0,0,WIDTH, HEIGHT);
			
			_text = TextFieldUtil.Generate(vo.Name.substr(0, 8), 14, 0x000000, FontProxy.FONT_YAHEI);
			_container.addChild(_text);			
			_text.x = int((WIDTH - _text.width)/2);
			_text.y = int(HEIGHT + 2);
			
			_coverLoader = new Loader();
			_container.addChild(_coverLoader);
			_coverLoader.x = 5;
			_coverLoader.y = 5;
			_coverLoader.load( new URLRequest(vo.CoverURLSmall) );
			
			this.addEventListener(MouseEvent.MOUSE_OVER, OnOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, OnOut);
		}
		
		/* Event Handler
		 * ---------------------------------------------------------- */
		private function OnOver(evt:MouseEvent):void{
			_bg.graphics.clear();
			_bg.graphics.beginFill(0x3c6995, 1);
			_bg.graphics.drawRect(0,0,WIDTH, HEIGHT);
		}
		
		private function OnOut(evt:MouseEvent):void{
			if(_isActive) return;
			_bg.graphics.clear();
			_bg.graphics.beginFill(0xffffff, 1);
			_bg.graphics.drawRect(0,0,WIDTH, HEIGHT);
		}
		
		public function get isActive():Boolean			{     return (_isActive);        }
		public function set isActive(value:Boolean):void{
			_isActive = value;
			if (_isActive){
				OnOver(null);
				mouseEnabled = false;
			} else {
				OnOut(null);
				mouseEnabled = true;
			};
		}
		public function get vo():OnlineResVO{	return _vo;	}
		
		public function Dispose():void
		{
			this.removeEventListener(MouseEvent.MOUSE_OVER, OnOver);
			this.removeEventListener(MouseEvent.MOUSE_OUT, OnOut);
		}
	}
}